<?php

namespace app\api\controller;

use think\Request;
use data\service\ArticleService;
use data\model\Article as articleModel;

class Article extends BaseApi
{

    private $article;

    protected function initialize()
    {
        parent::initialize();
        $this->article = new ArticleService();
    }

    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index(Request $request)
    {
        $page = $request->get('page',1);
        $size = $request->get('size',8);
        $extra = [];
        $request->get('type') && $extra['where'][] = ['type_id','=',$request->get('type')];
        $request->get('label') && $extra['where'][] = ['label_id','=',$request->get('label')];
        $request->get('key') && $extra['where'][] =['title','like','%'.$request->get('key').'%'];
        $extra['append'] = ['label_text'];
        list($count,$data) = array_values($this->article->getByPage($page,$size,'*',$extra));
        return $this->returnInfo(1,'',[
            'count'=>$count,
            'data'=>$data,
            'page'=>$page,
            'size'=>$size
        ]);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $post = $request->post();

        $validate = new \app\api\validate\Article;
        if (!$validate->check($post))
        {
            return $this->returnInfo(1,$validate->getError());
        }

        $post['cover'] || $post['cover'] = './public/uploads/default.jpg';  //设置默认的文章背景

        if ($this->article->add($post))
        {
            $url = $this->getRedirectUrl($post['type_id']);
            return $this->returnInfo(0,"添加成功!",['url'=>$url]);
        }else{
            return $this->returnInfo(1,$this->article->getError());
        }
    }

    /**
     * 获取返回的url
     */
    private function getRedirectUrl($typeId)
    {
        $usName = articleModel::getArticleTypeUs();

        return url('/admin/').$usName[$typeId];
    }


    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        $post = $request->put();

        $validate = new \app\api\validate\Article;
        if (!$validate->check($post))
        {
            return $this->returnInfo(1,$validate->getError());
        }

        if ($this->article->edit($id,$post))
        {
            $url = $this->getRedirectUrl($post['type_id']);
            return $this->returnInfo(0,"编辑成功!",['url'=>$url]);
        }else{
            return $this->returnInfo(1,$this->article->getError());
        }
    }

    /**
     * 修改字段
     */
    public function updateField(Request $request, $id)
    {
        $option = $request->only(['option'], 'put')['option'];
        if ($this->article->editOption($id,$option))
        {
            return $this->returnInfo(0,"编辑成功!");
        }else{
            return $this->returnInfo(1,$this->article->getError());
        }
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        if ($this->article->delete($id))
        {
            return $this->returnInfo(0,"删除成功!",['url'=>url('/admin/label')]);
        }else{
            return $this->returnInfo(1,$this->article->getError());
        }
    }


}
